package game.controller;

import game.basic.Util;
import javafx.scene.image.Image;

import java.util.HashMap;
import java.util.Random;

import static game.basic.GameBase.*;

public class Entity extends GameObject {

    protected static final Random random = new Random();

    public boolean invincible = false;//无敌状态
    public int invincibleCounter = 0;//无敌帧数计数器

    public int speed;//速度
    public int direction;//移动方向

    public int spriteNum = 0;//动画刷新的计数器,控制播放动画
    public int spriteCounter = 0;//移动帧数计数器

    public boolean collisionOn = false;//碰撞检测 false为没有碰撞到东西 不阻止移动

    public int actionLockCounter = 0;//固定帧数内行为不可改变 动作锁定计数器


    public int maxHp;
    public int hp;
    public int level=1;
    public int strength;//力量
    public int dexterity;//速度
    public int exp;
    public int nextLevelExp;
    public int coin;

    public int attack;//伤害
    public int defense;//防御



    //目前是什么状态 攻击的开关
    public boolean isAttacking = false;
    public int spriteAttack;//攻击帧数计数器


    //存活状态 false死亡
    public boolean alive = true;
    public boolean dying = false;//死亡动画
    public int dyingCounter = 0;//死亡帧数计数器
    public boolean hpBarOn = false;//血条显示 默认不显示
    public int hpBarCounter = 0;//血条计数器
    public Entity() {
    }

    public void readSpriteImage(String url, int width, int height, HashMap<Integer, Image[]> spriteMap) {

        Image[] up = new Image[ImgFps];
        Image[] down = new Image[ImgFps];
        Image[] left = new Image[ImgFps];
        Image[] right = new Image[ImgFps];

        String[] directions = {"up", "down", "left", "right"};

        int[] frames = {0, 1};

        for (String d : directions) {
            for (int frame : frames) {
                String imagePath = String.format(url, d, frame + 1);
                switch (d) {
                    case "up" -> up[frame] = Util.getImage(imagePath, width, height);
                    case "down" -> down[frame] = Util.getImage(imagePath, width, height);
                    case "left" -> left[frame] = Util.getImage(imagePath, width, height);
                    case "right" -> right[frame] = Util.getImage(imagePath, width, height);
                }
            }
        }
        spriteMap.put(upDir, up);
        spriteMap.put(downDir, down);
        spriteMap.put(leftDir, left);
        spriteMap.put(rightDir, right);
    }


    public void update() {
        if (!collisionOn) {
            switch (direction) {
                case upDir -> worldY -= speed;
                case downDir -> worldY += speed;
                case leftDir -> worldX -= speed;
                case rightDir -> worldX += speed;
            }
        }
        if (++spriteCounter % 12 == 0) { // 每10帧改变一次 一秒5帧
            spriteNum = (spriteNum + 1) % ImgFps;
            spriteCounter = 0;
        }
    }

    //设置行为
    public void setAction() {//设置移动行为
        actionLockCounter++;
        if (actionLockCounter >= 120) {
            int i = random.nextInt(100) + 1;//1-100
            int module = (i - 1) / 25 + 1; // 计算模块编号 将100分为4个模块
            switch (module) {
                case 1 -> direction = upDir;
                case 2 -> direction = downDir;
                case 3 -> direction = leftDir;
                case 4 -> direction = rightDir;
            }
            actionLockCounter = 0;
        }
    }

    public void speak() {
    }

    //损伤反应
    public void damageReaction() {
    }
}
